define(function (require, exports, module) {

    exports.init = function (ctx, uuid) {
        require("apps/grid");
        var page = new $.PageDomFactory(uuid);
        var pageListComponent = new $.PageListComponent(uuid, {
            view: {
                grid: "grid",
                myToolbar: "nsdb",
                colbar: "resource_template"
            },
            configs: {
                gridTitle: "权限列表",
                gridUrl: ctx + "/app/api/resource/queryResourceByPageForJson",
                gridColumns: [
                    {
                        field: 'id',
                        checkbox: true
//                        hidden: true
                    },
                    {
                        field: 'resourceName',
                        title: '资源名称',
                        width: 125,
                        align: 'left'
                    },
                    {
                        field: 'sysResource.resourceName',
                        title: '父级资源',
                        width: 125,
                        align: 'left',
                        formatter: function (value, row, index) {
                            if (value) {
                                return value;
                            } else {
                                return "<a>N/A</a>";
                            }
                        }
                    },
                    {
                        field: 'resourceType',
                        title: '资源类型',
                        width: 100,
                        align: 'left',
                        formatter: function (value, row, index) {
                            if (value == "menu") {
                                return "菜单";
                            } else {
                                return "其他";
                            }
                        }
                    },
                    {
                        field: 'available',
                        title: '状态',
                        width: 50,
                        align: 'left',
                        formatter: function (value, row, index) {
                            if (value == "1") {
                                return "启用";
                            } else {
                                return "禁用";
                            }
                        }
                    }
                ]
            },
            events: {
                renderColBarBefore: function (type, row) {
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "edit":
                            whenEdit(context, btn.href, row);
                            break;
                        case "view":
                            whenView(context, row);
                            break;
                        default :
                            break;
                    }
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        case "search_all":
                            context.searchAll();
                            break;
                        case "search":
                            context.search(btn.href, "资源搜索", "resource_search");
                            break;
                        case "del":
                            context.del(btn.href);
                            break;
                        case "add":
                            whenAdd(context, btn.href);
                            break;
                        default :
                            break;
                    }
                }
            }
        });

        var globalWindow = new $.PageWindow();

        function whenAdd(context, href) {
            globalWindow.create({
                href: href,
                method: "resource_add_save",
                title: "新增资源",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            globalWindow.open();
        }

        function whenEdit(context, href, row) {
            globalWindow.create({
                href: href + "?id=" + row.id,
                method: "resource_edit_save",
                title: "编辑资源",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            globalWindow.open();
        }

        function whenView(context, row) {
            context.detail("资源详情查看", row);
        }
    }
});